<template>
  <el-drawer direction="rtl" v-model="dialogVisible" title="用电统计" size="90%">
  <ContentWrap>
    <!-- 搜索工作栏 -->
    <el-form
      class="-mb-15px"
      :model="queryParams"
      ref="queryFormRef"
      :inline="true"
      label-width="68px"
    >
      <el-form-item label="年份" prop="year">
        <el-input
          v-model.number="queryParams.year"
          placeholder="请输入年份"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="月份" prop="month">
        <el-input
          v-model.number="queryParams.month"
          placeholder="请输入月份"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item>
        <el-button @click="handleQuery"><Icon icon="ep:search" class="mr-5px" /> 搜索</el-button>
        <el-button
          type="primary"
          plain
          @click="calc()"
        >
          <Icon icon="ep:plus" class="mr-5px" /> 重新统计该月份用电量
        </el-button>
      </el-form-item>
    </el-form>
  </ContentWrap>

  <!-- 列表 -->
  <ContentWrap>
    <el-table v-loading="loading" :data="list" :stripe="true" :show-overflow-tooltip="true">
      <el-table-column label="主键" align="center" prop="id" />
      <el-table-column label="地址名称" align="center" prop="addrName" width="180"/>
      <el-table-column label="地址编码" align="center" prop="addrCode" width="180"/>
      <el-table-column label="年份" align="center" prop="year" width="90"/>
      <el-table-column label="月份" align="center" prop="month" width="90"/>
      <el-table-column label="01日" align="center" prop="day01" width="90"/>
      <el-table-column label="02日" align="center" prop="day02" width="90"/>
      <el-table-column label="03日" align="center" prop="day03" width="90"/>
      <el-table-column label="04日" align="center" prop="day04" width="90"/>
      <el-table-column label="05日" align="center" prop="day05" width="90"/>
      <el-table-column label="06日" align="center" prop="day06" width="90"/>
      <el-table-column label="07日" align="center" prop="day07" width="90"/>
      <el-table-column label="08日" align="center" prop="day08" width="90"/>
      <el-table-column label="09日" align="center" prop="day09" width="90"/>
      <el-table-column label="10日" align="center" prop="day10" width="90"/>
      <el-table-column label="11日" align="center" prop="day11" width="90"/>
      <el-table-column label="12日" align="center" prop="day12" width="90"/>
      <el-table-column label="13日" align="center" prop="day13" width="90"/>
      <el-table-column label="14日" align="center" prop="day14" width="90"/>
      <el-table-column label="15日" align="center" prop="day15" width="90"/>
      <el-table-column label="16日" align="center" prop="day16" width="90"/>
      <el-table-column label="17日" align="center" prop="day17" width="90"/>
      <el-table-column label="18日" align="center" prop="day18" width="90"/>
      <el-table-column label="19日" align="center" prop="day19" width="90"/>
      <el-table-column label="20日" align="center" prop="day20" width="90"/>
      <el-table-column label="21日" align="center" prop="day21" width="90"/>
      <el-table-column label="22日" align="center" prop="day22" width="90"/>
      <el-table-column label="23日" align="center" prop="day23" width="90"/>
      <el-table-column label="24日" align="center" prop="day24" width="90"/>
      <el-table-column label="25日" align="center" prop="day25" width="90"/>
      <el-table-column label="26日" align="center" prop="day26" width="90"/>
      <el-table-column label="27日" align="center" prop="day27" width="90"/>
      <el-table-column label="28日" align="center" prop="day28" width="90"/>
      <el-table-column label="29日" align="center" prop="day29" width="90"/>
      <el-table-column label="30日" align="center" prop="day30" width="90"/>
      <el-table-column label="31日" align="center" prop="day31" width="90"/>
      <el-table-column
        label="创建时间"
        align="center"
        prop="createTime"
        :formatter="dateFormatter"
        width="180px"
      />
      <el-table-column label="操作" align="center">
        <template #default="scope">
          <el-button
            link
            type="danger"
            @click="handleDelete(scope.row.id)"
          >
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <Pagination
      :total="total"
      v-model:page="queryParams.pageNo"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />
  </ContentWrap>
</el-drawer>
</template>

<script setup lang="ts">
import { dateFormatter } from '@/utils/formatTime'
import { MeterUsePowerApi, MeterUsePowerVO } from '@/api/zwsj/meterusepower'

/** 用电量统计 列表 */
defineOptions({ name: 'MeterUserPowerCount' })

const message = useMessage() // 消息弹窗
const { t } = useI18n() // 国际化

const dialogVisible = ref(false) // 弹窗的是否展示
const loading = ref(true) // 列表的加载中
const list = ref<MeterUsePowerVO[]>([]) // 列表的数据
// 列表的总页数
const total = ref(0)
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  meterId: undefined,
  year: undefined,
  month: undefined,
})
const queryFormRef = ref() // 搜索的表单


/** 打开弹窗 */
const open = async (meterId: number) => {
  loading.value = false
  dialogVisible.value = true
  queryParams.meterId = meterId
  handleQuery()
}
defineExpose({ open }) // 提供 open 方法，用于打开弹窗


/** 查询列表 */
const getList = async () => {
  loading.value = true
  try {
    const data = await MeterUsePowerApi.getMeterUsePowerPage(queryParams)
    list.value = data.list
    total.value = data.total
  } finally {
    loading.value = false
  }
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.pageNo = 1
  getList()
}

/** 删除按钮操作 */
const handleDelete = async (id: number) => {
  try {
    // 删除的二次确认
    await message.delConfirm()
    // 发起删除
    await MeterUsePowerApi.deleteMeterUsePower(id)
    message.success(t('common.delSuccess'))
    // 刷新列表
    await getList()
  } catch {}
}

const calc = async () => {
  //判断年度和月份不能为空
  if(queryParams.year == undefined || queryParams.month == undefined){
    message.error('请选择具体年度和月份后计算')
    return ;
  }
  let tmpMonth = queryParams.month + "";
  if(queryParams.month < 10) tmpMonth = "0"+tmpMonth;
    await MeterUsePowerApi.createMeterUsePower({
      meterId: queryParams.meterId,
      year: queryParams.year,
      month: tmpMonth,
    })
    message.success('计算成功')
    handleQuery()
}
</script>
